# -*- coding: utf-8 -*-
"""
    Time    : 2020/12/23 6:03 下午
    Author  : Thinkgamer
    File    : 695-岛屿的最大面积.py
    Desc    : https://leetcode-cn.com/problems/max-area-of-island/
"""

"""
给定一个包含了一些 0 和 1 的非空二维数组 grid 。
一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合，这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设 grid 的四个边缘都被 0（代表水）包围着。
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿，则返回面积为 0 。)

示例 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]
对于上面这个给定矩阵应返回 6。注意答案不应该是 11 ，因为岛屿只能包含水平或垂直的四个方向的 1 。

示例 2:
[[0,0,0,0,0,0,0,0]]
对于上面这个给定的矩阵, 返回 0。
"""

"""
	思路v1：深度递归遍历
"""


def island(grid, i, j):
	# 判断边界 i 的边界为 0, rows, j 的边界为 0, columns
	if i in range(0, len(grid)) and j in range(0, len(grid[0])):
		if grid[i][j] == 0:
			return 0
		else:
			grid[i][j] = 0
			return 1 + island(grid, i, j-1) + island(grid, i, j + 1) + island(grid, i-1, j) + island(grid, i+1, j)
	else:
		return 0


# 256 ms 5.19% | 14.6 MB 75.10%
def max_area_of_island(grid):
	rows = len(grid)
	columns = len(grid[0])
	areas = 0
	for i in range(rows):
		for j in range(columns):
			areas = max(areas, island(grid, i, j))
	return areas


grid = [
	[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
	[0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
	[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
]

grid1 = [[0, 0, 0, 0, 0, 0, 0, 0]]

result = max_area_of_island(grid)
print(result)
